<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class forumAction extends commonAction
{
    public function forumList(){
        $whereMap=array('type'=>'child','state'=>'active');
        $count=D('forum')->where($whereMap)->count();
        list($pagesize,$offset,$pagestring)= pagestring($count, 5);
        $this->forum=D('forum')->where($whereMap)->limit($offset,$pagesize)->select();
        $this->assign('pagestring',$pagestring);
        $this->display();
    }
    public function getParentForum(){
        $whereMap=array('type'=>'parent','state'=>'active');
        $parentFroum=D('forum')->where($whereMap)->select();
        return $parentFroum;
    }
    public function forumEdit(){
        $whereMap=array('id'=>$_GET['forum_id'],'state'=>'active');
        $parentForum=$this->getParentForum();
        $this->assign('parentList',$parentForum);
        $this->forumInfo=D('forum')->where($whereMap)->find();
        $this->display();
    }
    public function editSave(){
        $arr=explode("-", $_POST['parent']);
        $whereMap=array('id'=>$_POST['forum_id'],'state'=>'active');
        $data=array('forum_name'=>$_POST['forum_name'],'parent_forum_id'=>$arr[0],'parent_forum_name'=>$arr[1]);
        D('forum')->where($whereMap)->data($data)->save();
        redirect("forumEdit?forum_id={$_POST['forum_id']}");
    }
    public function forumAdd(){
        if($_GET['flag']=='success'){
            $this->assign('flag','<font color=green>创建父版块成功</font>');
        }
        if($_GET['msg']=='success'){
            $this->assign('msg','<font color=green>增加子版块成功</font>');
        }
        $parentForum=$this->getParentForum();
        $this->assign('parentForum',$parentForum);
        $this->display();
    }
    public function addSave(){
        $arr=explode("-", $_POST['parent_forum']);
        $data=array(
            'parent_forum_id'=>$arr[0],
            'parent_forum_name'=>$arr[1],
            'forum_name'=>$_POST['forum_name'],
            'type'=>'child'
            );
        D('forum')->data($data)->add();
        $this->assign('msg',"<font color=green>增加版块成功</font>");
        redirect("forumAdd?msg=success");
    }
    public function createParentForum(){
        $data=array(
            'forum_name'=>$_POST['forum_name'],
            'type'=>'parent'
            );
        $res=D('forum')->data($data)->add();
        if($res){
            redirect('forumAdd?flag=success');
        }
    }
    public function forumDrop(){
        $whereMap=array(
            'id'=>$_GET['forum_id'],
        );
        D('forum')->where($whereMap)->setField('state', 'disable');
        redirect('forumList');
    }
    public function forumDelete(){
        $parentForum=$this->getParentForum();
        $this->assign('parentForum',$parentForum);
        if($_POST['forum_id']){
            $whereMap=array(
                'id'=>$_POST['forum_id'],
            );
            $res_p=D('forum')->where($whereMap)->setField('state', 'disable');
            $res_c=D('forum')->where("parent_forum_id={$_POST['forum_id']}")->setField('state', 'disable');
            if($res_p||$res_c){
                $this->assign('msg',"<font color='green'>删除成功</font>");
                $this->display();
                exit();
            }
            $this->display();
            exit();
        }
        $this->display();
    }
    
    public function getChildByParent(){
        $whereMap=array('parent_forum_id'=>$_POST['forum_id'],'state'=>'active','type'=>'child');
        $res=D('forum')->where($whereMap)->select();
        $this->ajaxReturn($res,'JSON');//无需手动转换成json数据
    }
    //数据库备份
     public function dataBack(){
         if($_GET['flag']=='success'){
               $this->assign('flag',"<font color='green'>数据恢复成功</font>");
            }elseif($_GET['flag']=='fail'){
               $this->assign('flag',"<font color='red'>数据恢复失败</font>"); 
            }
        if($_POST['filename']){
            $filename=$_POST['filename'];
            $dataPath='./databack/'.$filename.'.sql';
            $host=C('DB_HOST');
            $user=C('DB_USER');
            $pwd=C('DB_PWD');
            $dbname=C('DB_NAME');
            $sql="mysqldump -h".$host." -u".$user." -p".$pwd." ".$dbname." > ".$dataPath;
            $res=exec($sql);
            if(!$res){
                $this->assign('msg',"<font color='green'>备份数据成功</font>");
                $this->display();
                exit();
            }  else {
                $this->assign('msg',"<font color='red'>备份数据失败</font>");
                $this->display();
                exit();
            }
            
         }
       
        $this->display();

    }
  
    public function getFileList(){
        $dir='./databack';
        $currdir=opendir($dir);
        while($currentFile!==false){
            $currentFile=readDir($currdir);
            if($currentFile!=='.'&&$currentFile!=='..'&&$currentFile!==false){
                $files[] = $currentFile;
            }
        }
        //$files=  scandir($dir,2);
        echo json_encode($files);
    }
     public function dataRecover(){
         if($_POST['databack']){
            $filename=$_POST['databack'];
            $dataPath='./databack/'.$filename.'.sql';
            $host=C('DB_HOST');
            $user=C('DB_USER');
            $pwd=C('DB_PWD');
            $dbname=C('DB_NAME');
            $sql="mysql -h".$host." -u".$user." -p".$pwd." ".$dbname." < ".$dataPath;//print_r($sql);exit();
            $res=exec($sql);
            if(!$res){
                //$this->assign('flag',"success");
                header("location:dataBack?flag=success");
            }else{
                header("location:dataBack?flag=fail");;
                //$this->assign('flag',"fail");
            }
         }
         $this->display('dataBack');
     }
    
}
?>
